<?php

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run() {
        $this->call('LocationSeeder');
        $this->call('RolePermissionSeeder');
        $this->call('OptionSeeder');
        $this->call('UserSeeder');

        Cache::flush();

        if (!is_dir(public_path() . "/Temp")) {
            mkdir(public_path() . "/Temp", 0777);
        }

        if (!is_dir(public_path() . "/profile-image")) {
            mkdir(public_path() . "/profile-image", 0777);
        }

        $this->command->info('User table seeded!');
    }

}

class LocationSeeder extends Seeder {

    public function run() {
        DB::table('district')->truncate();
        DB::table('province')->truncate();
        DB::table('ward')->truncate();

        $connectionInfo = Config::get('database.connections.mysql');
        $sqlPath        = __DIR__ . '/../district_ward_province.sql';

        system(sprintf('mysql -u%s -p%s -h%s %s < \'%s\'', $connectionInfo['username'], $connectionInfo['password'], $connectionInfo['host'], $connectionInfo['database'], $sqlPath));
    }

}

class RolePermissionSeeder extends Seeder {

    public function run() {
        DB::table('roles')->truncate();
        DB::table('permissions')->truncate();
        DB::table('permission_role')->truncate();
        DB::table('assigned_roles')->truncate();

        // <editor-fold defaultstate="collapsed" desc="Roles Permissions">
        DB::insert(
            "INSERT INTO `roles` (`type`, `name`, `updater`, `created_at`, `updated_at`) VALUES
            ('GROUP', 'Quản Trị', 'admin', '2014-04-22 09:02:34', '2014-04-22 09:02:34');"
        );

        DB::insert(
            "INSERT INTO `permissions` (`name`, `display_name`, `updater`, `created_at`, `updated_at`) VALUES
            ('manage-User', 'Quản Lý Tài Khoản', 'admin', '2014-04-22 02:03:24', '2014-04-22 02:03:24'),
            ('manage-Class', 'Quản Lý Lớp Học', 'admin', '2014-04-24 03:37:12', '2014-04-24 03:37:12'),
            ('manage-User-Create', 'Tạo Tài Khoản', 'admin', '2014-04-24 03:39:56', '2014-04-24 03:39:56'),
            ('manage-User-Attendance', 'Điểm Danh', 'admin', '2014-04-24 03:48:04', '2014-04-24 03:51:25'),
            ('manage-User-Attendance-Score', 'Ghi Điểm', 'admin', '2014-04-24 03:48:56', '2014-04-24 03:52:38'),
            ('manage-User-Review', 'Đánh Giá Cuối Năm', 'admin', '2014-04-24 03:49:33', '2014-04-24 03:52:02'),
            ('manage-Document', 'Quản Lý Tài Liệu', 'admin', '2014-04-24 03:49:33', '2014-04-24 03:52:02'),
            ('permission', 'Phân Quyền', 'admin', '2014-04-24 03:50:08', '2014-04-24 03:50:08'),
            ('system-option', 'Thông Số Hệ Thống', 'admin', '2014-04-24 03:50:34', '2014-04-24 03:50:34');"
        );
        // </editor-fold>
    }

}

class OptionSeeder extends Seeder {

    public function run() {
        DB::table('options')->truncate();

        // <editor-fold defaultstate="collapsed" desc="Options">
        DB::insert(
            "INSERT INTO `options` (`type`, `key`, `data`) VALUES 
            ('USERTYPE', 'QUAN_TRI', 'Quản Trị'),
            ('USERTYPE', 'THI_SINH', 'Thí Sinh'),
            ('USERTYPE', 'TINH_NGUYEN_VIEN', 'Tình Nguyện Viên'),
            ('RELATIONSHIP', 'CHA', 'Cha'),
            ('RELATIONSHIP', 'ME', 'Mẹ'),
            ('RELATIONSHIP', 'ONG_BA', 'Ông/Bà'),
            ('RELATIONSHIP', 'CO_CHU', 'Cô/Chú'),
            ('RELATIONSHIP', 'OTHER', 'Trường Hợp Khác');"
        );
        // </editor-fold>
    }

}

class UserSeeder extends Seeder {

    public function run() {

        DB::table('user')->truncate();
        DB::table('user_dear')->truncate();

        // <editor-fold defaultstate="collapsed" desc="User">
        DB::table('user')->insert(array(
            array(
                //Admin
                "id"                => 'admin',
                "passwd"            => Hash::make('1'),
                "usertype"          => "QUAN_TRI",
                "userstatus"        => "",
                "saint"             => "Giuse",
                "username"          => "Hồ Minh Long",
                "firstname"         => "Long",
                "gender"            => "1",
                "birthday"          => "1988-07-09",
                "usercrew"          => "",
                "baptism_date"      => "",
                "eucharist_date"    => "",
                "confirmation_date" => "",
                "email"             => "hominhlong.it@gmail.com",
                "phone"             => "",
                "address"           => "",
                "parish"            => "",
                "gencode"           => "",
                "note"              => "",
                "updater"           => "",
                "created_at"        => date('Y-m-d H:i:s'),
                "updated_at"        => date('Y-m-d H:i:s'),
            )
        ));

        for ($i = 0; $i < 100; $i++) {

            $faker = Faker\Factory::create();

            DB::table('user')->insert(array(
                array(
                    "id"                => CNamHoa::generateUserID('QUAN_TRI'),
                    "passwd"            => Hash::make('1'),
                    "usertype"          => "QUAN_TRI",
                    "userstatus"        => "",
                    "saint"             => $faker->firstname,
                    "username"          => $faker->name,
                    "firstname"         => $faker->firstname,
                    "gender"            => "1",
                    "birthday"          => $faker->datetime,
                    "usercrew"          => "",
                    "baptism_date"      => $faker->datetime,
                    "eucharist_date"    => $faker->datetime,
                    "confirmation_date" => $faker->datetime,
                    "email"             => $faker->companyEmail,
                    "phone"             => $faker->phoneNumber,
                    "address"           => $faker->address,
                    "parish"            => "",
                    "gencode"           => "",
                    "note"              => "",
                    "updater"           => "",
                    "created_at"        => date('Y-m-d H:i:s'),
                    "updated_at"        => date('Y-m-d H:i:s'),
                )
            ));

            DB::table('user')->insert(array(
                array(
                    "id"                => CNamHoa::generateUserID('TINH_NGUYEN_VIEN'),
                    "passwd"            => Hash::make('1'),
                    "usertype"          => "TINH_NGUYEN_VIEN",
                    "userstatus"        => "",
                    "saint"             => $faker->firstname,
                    "username"          => $faker->name,
                    "firstname"         => $faker->firstname,
                    "gender"            => "1",
                    "birthday"          => $faker->datetime,
                    "usercrew"          => "",
                    "baptism_date"      => $faker->datetime,
                    "eucharist_date"    => $faker->datetime,
                    "confirmation_date" => $faker->datetime,
                    "email"             => $faker->companyEmail,
                    "phone"             => $faker->phoneNumber,
                    "address"           => $faker->address,
                    "parish"            => "",
                    "gencode"           => "",
                    "note"              => "",
                    "updater"           => "",
                    "created_at"        => date('Y-m-d H:i:s'),
                    "updated_at"        => date('Y-m-d H:i:s'),
                )
            ));

            DB::table('user')->insert(array(
                array(
                    "id"                => CNamHoa::generateUserID(),
                    "passwd"            => Hash::make('1'),
                    "usertype"          => "THI_SINH",
                    "userstatus"        => "",
                    "saint"             => $faker->firstname,
                    "username"          => $faker->name,
                    "firstname"         => $faker->firstname,
                    "gender"            => "1",
                    "birthday"          => $faker->datetime,
                    "usercrew"          => "",
                    "baptism_date"      => $faker->datetime,
                    "eucharist_date"    => $faker->datetime,
                    "confirmation_date" => $faker->datetime,
                    "email"             => $faker->companyEmail,
                    "phone"             => $faker->phoneNumber,
                    "address"           => $faker->address,
                    "parish"            => "",
                    "gencode"           => "",
                    "note"              => "",
                    "updater"           => "",
                    "created_at"        => date('Y-m-d H:i:s'),
                    "updated_at"        => date('Y-m-d H:i:s'),
                )
            ));
        }
        //</editor-fold>
    }

}
